package com.kele.demo.utils;

import com.kele.demo.config.EnvironmentModule;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;

/**
 * @author chunhui.li
 * @date 2022/2/12 15:55
 * @Description
 * @ClassName BeanUtil
 */
public class BeanUtil {

    //将管理上下文的applicationContext设置成静态变量，供全局调用
    public static ConfigurableApplicationContext applicationContext;
    //定义一个获取已经实例化bean的方法
    public static <T> T getBean(Class<T> c){
        return applicationContext.getBean(c);
    }
    //定义一个获取已经实例化bean的方法
    public static <T> T getBeanByName(String beanName,Class<T> c){
        return applicationContext.getBean(beanName,c);
    }

//    /**
//     *获得注入的环境组件
//     */
//    @Bean(name = "serviceEnv")
//    public static EnvironmentModule getEnv() {
//        Environment environment = applicationContext.getBean(Environment.class);
//        EnvironmentModule environmentModule = new EnvironmentModule();
//        environmentModule.setPort(environment.getProperty("server.port"));
//        environmentModule.setServiceId(environment.getProperty("spring.application.name"));
//        return environmentModule;
//    }

}
